package com.xqboss.apps.service.user;


import com.xqboss.apps.domain.user.UserIdentityPermWrenchPresenter;
import com.xqboss.apps.domain.user.UserIdentityRank;
import com.xqboss.apps.mapper.user.UserIdentityRankMapper;
import com.xqboss.apps.vo.user.UserIdentityPermIntegralVo;
import com.xqboss.apps.vo.user.UserIdentityPermWrenchPresenterVo;
import com.xqboss.common.core.domain.entity.SysUser;
import com.xqboss.common.exception.ServiceException;
import com.xqboss.common.utils.NumberUtils;
import com.xqboss.common.utils.bean.BeanUtils;
import com.xqboss.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.user.UserIdentityPermIntegral;
import com.xqboss.apps.mapper.user.UserIdentityPermIntegralMapper;
import org.springframework.transaction.annotation.Transactional;


/**
 * 用户身份-权限-热豆转赠(UserIdentityPermIntegral)表服务实现类
 *
 * @author makejava
 * @since 2023-12-07 13:59:34
 */
@Service
public class UserIdentityPermIntegralService extends ServiceImpl<UserIdentityPermIntegralMapper, UserIdentityPermIntegral> {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private UserIdentityRankMapper userIdentityRankMapper;
    @Autowired
    private UserIdentityPermWrenchPresenterService userIdentityPermWrenchPresenterService;

    /**
     * 热豆转赠配置：
     *
     * - 转赠开关：默认选中【关闭】，关闭后该身份不能发起转赠，关闭后转赠开关、服务费、每日转赠次数、单笔转赠区间字段不显示
     * - 服务费比例：转赠服务费 ＝ 单笔转赠数量 × 服务费比例，0~100整数
     * - 每日转赠次数：限制该身份每日转赠次数，每日24点后重置；转卖次数做累计，例：升级前转卖次数5次，已使用2次，转卖后转卖次数10次，当前剩余转卖次数：10－2 ＝ 8
     * - 单笔转赠区间：正整数，最高数量大于等于最低数量
     *
     * @param vo 请求对象
     */
    @Transactional(rollbackFor=Exception.class)
    public void hotBeanGift(UserIdentityPermIntegralVo vo) {
        if (vo.getIsOpen() == 1) {
            if (vo.getMaxDailyTimes() == null || vo.getMaxSingleAmount() == null || vo.getMinSingleAmount() == null) {
                throw new ServiceException("转赠参数不完整！");
            }
            if(!NumberUtils.isPositiveInteger(String.valueOf(vo.getFeeRate()))){
                throw new ServiceException("服务费比例必须为正整数！");
            }
            if(!NumberUtils.isPositiveInteger(String.valueOf(vo.getMaxDailyTimes()))){
                throw new ServiceException("每日最大转赠次数必须为正整数！");
            }
            if(!NumberUtils.isPositiveInteger(String.valueOf(vo.getMinSingleAmount()))){
                throw new ServiceException("单笔转赠最低数量必须为正整数！");
            }
            if(!NumberUtils.isPositiveInteger(String.valueOf(vo.getMaxSingleAmount()))){
                throw new ServiceException("单笔转赠最高数量必须为正整数！");
            }
            if(vo.getMaxSingleAmount().compareTo(vo.getMinSingleAmount()) < 0){
                throw new ServiceException("单笔转赠最高数量必须大于等于单笔转赠最低数量！");
            }
        }
        UserIdentityPermIntegral permIntegral = new UserIdentityPermIntegral();
        BeanUtils.copyBeanProp(permIntegral,vo);
        this.saveOrUpdate(permIntegral);
    }

    /**
     * 获取热豆转赠
     *
     * @param identityId 身份id
     */
    public UserIdentityPermIntegral hotBeanGiftInfo(Long identityId) {
        UserIdentityPermIntegral one = this.lambdaQuery().eq(UserIdentityPermIntegral::getIdentityId, identityId).one();
        return one==null?new UserIdentityPermIntegral():one;
    }

    /**
     * 判断是否开启热豆转赠
     * @param userId
     * @return
     */
    public Boolean getIsOpen(Long userId) {
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        Long identityRankId = sysUser.getIdentityRankId();
        UserIdentityRank userIdentityRank = userIdentityRankMapper.selectById(identityRankId);
        Long identityId = userIdentityRank.getIdentityId();
        UserIdentityPermIntegral one = this.lambdaQuery().eq(UserIdentityPermIntegral::getIdentityId, identityId).last("limit 1").one();
        return (one==null?0:one.getIsOpen())!=0;
    }

    /**
     * 根据用户身份获取扳手转增配置
      * @param identityId
     * @return
     */
    public UserIdentityPermWrenchPresenter wrenchGiftInfo(Long identityId) {
        UserIdentityPermWrenchPresenter one = userIdentityPermWrenchPresenterService.lambdaQuery().eq(UserIdentityPermWrenchPresenter::getIdentityId, identityId).one();
        return one == null ? new UserIdentityPermWrenchPresenter() : one;
    }

    /**
     * 扳手转增修改
     * @param vo
     */
    @Transactional(rollbackFor=Exception.class)
    public void wrenchGift(UserIdentityPermWrenchPresenterVo vo) {
        if (vo.getIsOpen() == 1) {
            if (vo.getMaxDailyTimes() == null || vo.getMaxSingleAmount() == null || vo.getMinSingleAmount() == null) {
                throw new ServiceException("转赠参数不完整！");
            }
            if(!NumberUtils.isPositiveInteger(String.valueOf(vo.getFeeRate()))){
                throw new ServiceException("服务费比例必须为正整数！");
            }
            if(!NumberUtils.isPositiveInteger(String.valueOf(vo.getMaxDailyTimes()))){
                throw new ServiceException("每日最大转赠次数必须为正整数！");
            }
            if(!NumberUtils.isPositiveInteger(String.valueOf(vo.getMinSingleAmount()))){
                throw new ServiceException("单笔转赠最低数量必须为正整数！");
            }
            if(!NumberUtils.isPositiveInteger(String.valueOf(vo.getMaxSingleAmount()))){
                throw new ServiceException("单笔转赠最高数量必须为正整数！");
            }
            if(vo.getMaxSingleAmount().compareTo(vo.getMinSingleAmount()) < 0){
                throw new ServiceException("单笔转赠最高数量必须大于等于单笔转赠最低数量！");
            }
        }
        UserIdentityPermWrenchPresenter presenter = new UserIdentityPermWrenchPresenter();
        BeanUtils.copyBeanProp(presenter,vo);
        userIdentityPermWrenchPresenterService.saveOrUpdate(presenter);
    }
}
